class ProductsController < ApplicationController
  # GET /products
  # GET /products.xml
  
  before_filter :login_required
  before_filter :session_expiry
  
  def index
    @products = Product.paginate :page => params[:page], :per_page => 10, :order => 'name'

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @products }
    end
  end

  # GET /products/1
  # GET /products/1.xml
  def show
    @product = Product.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/new
  # GET /products/new.xml
  def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.xml
  def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        flash[:notice] = 'Product was successfully created.'
        format.html { redirect_to(@product) }
        format.xml  { render :xml => @product, :status => :created, :location => @product }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.xml
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        flash[:notice] = 'Product was successfully updated.'
        format.html { redirect_to(@product) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.xml
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to(products_url) }
      format.xml  { head :ok }
    end
  end
  
  def filter
    if (params[:query] and params[:param_table])
	  @query = params[:query]
	  @param_table = params[:param_table]
	else
    
    @query = ""
    @param_table = []
    
    @query = @query + "name like ? "
    @param_table << "%"+params[:filter][:name]+"%"
	
	@query = @query + "AND code like ? "
    @param_table << "%"+params[:filter][:code]+"%"
	
	@query = @query + "AND pkwiu like ? "
    @param_table << "%"+params[:filter][:pkwiu]+"%"
	
	@query = @query + "AND unit like ? "
    @param_table << "%"+params[:filter][:unit]+"%"
    
    if params[:filter][:net_price] != ""
        @query = @query + "AND net_price = ? "
        @param_table << params[:filter][:net_price]
    end
    
    if params[:filter][:vat_rate] != "" 
        @query = @query + "AND vat_rate = ? "
        @param_table << params[:filter][:vat_rate]
    end
	
	end
    
	flash[:query] = @query
	flash[:param_table] = @param_table
    @products = Product.paginate :conditions => [@query, *@param_table], :page => params[:page], :per_page => 10, :order => 'name'
  end
end
